package com.lxb.vioce.view;

import android.app.ActivityOptions;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.lxb.vioce.R;
import com.lxb.vioce.base.BaseActivity;
import com.lxb.vioce.model.SpeechResultBean;
import com.lxb.vioce.utils.GsonUtil;
import com.lxb.vioce.utils.StringUtil;

import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.shareView)
    ImageView shareView;
    @ViewInject(R.id.speechWordsTv)
    TextView speechWordsTv;
    @ViewInject(R.id.scan_goods)
    Intent serviceIntent;
    ImageView scan_goods;
    MsgReceiver msgReceiver;

    @Override
    protected void initView() {
        // 设置加载图片的参数
        ImageOptions options = new ImageOptions.Builder()
                // 是否忽略GIF格式的图片
                .setIgnoreGif(false)
                // 图片缩放模式
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                // 得到ImageOptions对象
                .build();
        x.image().bind(shareView, "assets://voice_gif.gif", options);

        //动态注册广播接收器
        msgReceiver = new MsgReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.communication.RECEIVER");
        registerReceiver(msgReceiver, intentFilter);

        serviceIntent = new Intent(this, SpeechService.class);
        startService(serviceIntent);
        scan_goods.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.scan_goods:

                break;
        }
    }

    /**
     * 广播接收器
     *
     * @author len
     */
    public class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String words = intent.getStringExtra("speech");
            speechWordsTv.setText(words);
            if (intent.getBooleanExtra("isLast", false)) {
//                Intent activityIntent = new Intent(MainActivity.this, OtherActivity.class);
//                startActivity(activityIntent,
//                        ActivityOptions.makeSceneTransitionAnimation(MainActivity.this,
//                                shareView, "shareName").toBundle());
                Toast.makeText(MainActivity.this, "跳转", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(serviceIntent);
        unregisterReceiver(msgReceiver);
    }
}
